<template>
	<view>
		<!-- 		<u-navbar back-text="登录" :customBack="back" :borderBottom="false" :background="background"
			:back-text-style="{'color':'#fff'}" backIconColor="#fff"></u-navbar> -->
		<view class="home center">
			<view>
				<!-- logo -->
				<!-- <u-image src="../../static/logo.png" height="450" borderRadius="12"></u-image> -->
				<image src="../../static/images/logo_new.png" style="height: 600upx;" mode=""></image>
				<view style="color: gray;margin-top: -50upx;">· 你想要的源码都在这里</view>
				

				<!-- <image src="../../static/images/yuanmahui.png" style="width: 400upx;height: 400upx;" mode=""></image> -->
			</view>
			<view style="margin-top: 80upx;">
				<u-button type="success" shape="circle" @click="wxLogin()" >微信一键登录</u-button>
			</view>
			
			

		<!-- 	<view style="margin-top: 40upx;">

				<button style="width: 100%;background-color: #F8D55D;border-radius: 40upx;" open-type="getUserInfo"
					shape="circle" @click="qqLogin()">授权登录</button>
			</view> -->
		</view>
	</view>
</template>

<script>
	// import {login} from '../../api/user.js'
	import {
		login_code,
		qq_login_code,
		user_info
	} from '../../api/auth.js'
	import {
		set
	} from '../../utils/local.js'

	import {
		get
	} from '../../utils/local.js'
	export default {
		data() {
			return {
				// background: {
				// 	'background-image': 'linear-gradient(45deg, rgb(28, 187, 180), rgb(141, 198, 63))'
				// },
				loginForm: {}
			}
		},
		onLoad() {

		},
		methods: {
			qqLogin() {
				var that = this;
				let code = '';
				uni.login({
					provider: "qq",
					success(res) {
						console.log(res)
						code = res.code
					}
				})
				uni.getUserProfile({
					provider: 'qq',
					lang: 'zh_CN',
					desc: "用于获取用户信息",
					success: (result) => {
						console.log(result)
						let params = {}
						params.code = code;
						params.nickName = result.userInfo.nickName;
						params.avatarUrl = result.userInfo.avatarUrl;
						qq_login_code(params).then(res => {
							console.log(res.data)
							set('X-Token', res.data)
							that.getUserInfo()
							setTimeout(function() {
								uni.switchTab({
									url: '../user/user'
								})
							}, 100)
						})
					}
				})


			},



			wxLogin() {
				var that = this;
				let code = '';
				uni.login({
					provider: "weixin",
					success(res) {
						code = res.code
					}
				})
				uni.getUserProfile({
					provider: 'weixin',
					lang: 'zh_CN',
					desc: "用于获取用户信息",
					success: (result) => {
						console.log(result.userInfo)
						let params = {}
						params.code = code;
						params.wxUserInfoRequest = result.userInfo;
						login_code(params).then(res => {
							console.log(res.data)
							set('X-Token', res.data)
							that.getUserInfo()
							setTimeout(function() {
								// uni.navigateTo({
								// 	url: '../user/user'
								// })
								uni.switchTab({
									url: '../user/user'
								})
							}, 100)
						})
					}
				})
			},

			getUserInfo() {
				let token = get('X-Token')
				console.log("X-Token:" + token)
				if (token) {
					this.isLogin = true
					user_info().then(res => {
						console.log(res)
						this.userInfo = res.data
						set('userInfo', JSON.stringify(res.data))
					})

				} else {
					this.isLogin = false
					this.userInfo = {}
				}

			},

			back() {
				return this.$common.switchTabTo('/pages/index/index')
			},
			userLogin() {
				// 登录方法
				if (this.loginForm.mobile === '' || this.loginForm.mobile === null) {
					uni.showToast({
						icon: 'none',
						title: '请输入手机号'
					})
					return
				}
				if (this.loginForm.password === '' || this.loginForm.password === null) {
					uni.showToast({
						icon: 'none',
						title: '请输入密码'
					})
					return
				}
				login(this.loginForm).then(res => {
					if (res.status === 200) {
						console.log(res)
						uni.showToast({
							title: '登录成功',
							duration: 2000
						})
						set('userId', res.data.userId)
						set('avatar', res.data.avatar)
						set('nickname', res.data.nickname)
						set('token', res.data.token)

						uni.switchTab({
							url: '../mine/index'
						})


					}
				})
			},

			toRegister() {
				uni.navigateTo({
					url: '../register/register'
				})
			},

		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fff;
	}

	.center {
		text-align: center;
	}

	.wxname {
		// padding-bottom: 20rpx;
		font-size: 38rpx;
		font-weight: bold;
	}
</style>
